package com.example.bluetooth_client;

import java.lang.reflect.Method;
import java.util.UUID;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity_client extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity_client);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_activity_client, menu);
        return true;
    }

    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
		registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST));	

    }
    private final UUID my_UUID = UUID.fromString("00001802-0000-1000-8000-00805f9b34fb");
	  final BroadcastReceiver bReceiver = new BroadcastReceiver() {
		    @SuppressLint("NewApi") public void onReceive(Context context, Intent intent) {
		        String action = intent.getAction();
		        // When discovery finds a device
		        if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) {
		             // Get the BluetoothDevice object from the Intent
		        	 BluetoothDevice btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
		        	 btDevice.setPairingConfirmation(true);
		        	 String abc="123";
		        	 byte[]aa=abc.getBytes();
		        	 btDevice.setPin(aa);
		        	 try {
			             Log.d("pairDevice()", "Start Pairing...");
				            Method m = btDevice.getClass().getMethod("createBond", (Class[]) null);
				            Boolean returnValue = (Boolean) m.invoke(btDevice, (Object[]) null);
				            Log.d("pairDevice()", "Pairing finished.");
				       
						
					} catch (Exception e) {
						// TODO: handle exception
					}
		        	 /*		        	 device_list.add(device);
		        	 // add the name and the MAC address of the object to the arrayAdapter
		             BTArrayAdapter.add(device.getName() + "\n" + device.getAddress());
		             BTArrayAdapter.notifyDataSetChanged();
*/		        }
		    }
		};
		

}
